<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运输单位" prop="transportUnit">
        <el-select v-model="queryParams.transportUnit" filterable placeholder="请选择运输单位" :default-first-option="true">
          <el-option label="全部" value="">全部</el-option>
          <el-option v-for="dict in carrierList" :key="dict.carrierId" :label="dict.carrierName"
            :value="parseInt(dict.carrierId)" clearable @keyup.enter.native="handleQuery"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号/驾驶员" prop="numberPlate" label-width="130px">
        <el-input v-model="queryParams.numberPlate" placeholder="请输入车牌号/驾驶员" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary " icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['basic:info:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['basic:info:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['basic:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['basic:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" width="68" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="运输单位" align="center" prop="transportUnit" width="300" />
      <el-table-column label="车牌号" align="center" prop="numberPlate" width="180" />
      <el-table-column label="驾驶员" align="center" prop="driver" />
      <el-table-column label="有效承重(t)" align="center" prop="loadBearing" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:info:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['basic:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="运输单位" prop="transportUnit">
              <el-select v-model="form.transportUnit" filterable placeholder="请选择运输单位">
                <el-option v-for="dict in carrierList" :key="dict.carrierId" :label="dict.carrierName"
                  :value="parseInt(dict.carrierId)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车牌号" prop="numberPlate">
              <el-input v-model="form.numberPlate" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="驾驶员" prop="driver">
              <el-input v-model="form.driver" placeholder="请输入驾驶员" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="营运证号码" prop="operatingLicense">
              <el-input v-model="form.operatingLicense" placeholder="请输入营运证号码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车厂家型号" prop="manufacturerModel">
              <el-input v-model="form.manufacturerModel" placeholder="请输入车厂家型号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发动机号" prop="engineNumber">
              <el-input v-model="form.engineNumber" placeholder="请输入发动机号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="有效承重(t)" prop="loadBearing">
              <el-input v-model="form.loadBearing" placeholder="请输入有效承重" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆编号" prop="vehicleNumber">
              <el-input v-model="form.vehicleNumber" placeholder="请输入车辆编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行驶证号码" prop="driverLicense">
              <el-input v-model="form.driverLicense" placeholder="请输入行驶证号码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="生产厂家" prop="manufacturer">
              <el-input v-model="form.manufacturer" placeholder="请输入生产厂家" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="驾驶员身份证" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入驾驶员身份证" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车容量编码" prop="carCapacity">
              <el-input v-model="form.carCapacity" placeholder="请输入车容量编码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="实际车长(m)" prop="carLength">
              <el-input v-model="form.carLength" placeholder="请输入实际车长" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="实际车宽(m)" prop="carWidth">
              <el-input v-model="form.carWidth" placeholder="请输入实际车宽" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="底盘高度(m)" prop="chassisHeight">
              <el-input v-model="form.chassisHeight" placeholder="请输入底盘高度" />
            </el-form-item>

          </el-col>

        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="所在城市" prop="city">
              <el-cascader v-model="form.city" filterable :options="citys"
                :props="{ label: 'label', value: 'label', children: 'children' }" placeholder="请选择所在城市"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="购置费证号码" prop="pfcNumber">
              <el-input v-model="form.pfcNumber" placeholder="请输入购置费证号码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车架号码" prop="frameNumber">
              <el-input v-model="form.frameNumber" placeholder="请输入车架号码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="infoRow">
          <el-col :span="8">
            <el-form-item label="车身颜色" prop="carColor">
              <el-color-picker v-model="form.carColor" size="medium" color-format="hex"></el-color-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大数量" prop="maxNumber">
              <el-input v-model="form.maxNumber" placeholder="请输入最大数量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行车里程(km)" prop="mileage">
              <el-input v-model="form.mileage" placeholder="请输入行车里程" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, listCarrier, getCarrier } from "@/api/basic/info";
import city from "@/api/basic/city"
export default {
  inheritAttrs: false,
  props: [],
  name: "Info",
  data() {
    return {
      // formData: {
      //   field104: '#000000',
      // },
      rules: {},
      citys: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      infoList: [],
      // 承运商信息表格数据
      carrierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        numberPlate: null,
        transportUnit: ""
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        transportUnit: [
          { required: true, message: "运输单位不能为空", trigger: "blur" }
        ],
        driver: [
          { required: true, message: "驾驶员不能为空", trigger: "blur" }
        ],
        numberPlate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        operatingLicense: [
          { required: true, message: "营运证号码不能为空", trigger: "blur" }
        ],
        chassisHeight: [
          { required: true, message: "底盘高度不能为空", trigger: "blur" }
        ],
        loadBearing: [
          { required: true, message: "有效承重不能为空", trigger: "blur" }
        ],
        driverLicense: [
          { required: true, message: "行驶证号码不能为空", trigger: "blur" }
        ],
        manufacturer: [
          { required: true, message: "生产厂家不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "驾驶员身份证不能为空", trigger: "blur" }
        ],
        carLength: [
          { required: true, message: "实际车长不能为空", trigger: "blur" }
        ],
        maxNumber: [
          { required: true, message: "最大数量不能为空", trigger: "blur" }
        ],
        carWidth: [
          { required: true, message: "实际车宽不能为空", trigger: "blur" }
        ],
        mileage: [
          { required: true, message: "行车里程不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆信息列表 */

    getList() {
      this.citys = city.city;
      console.log(this.citys, "city")
      this.loading = true;

      listInfo(this.queryParams).then(response => {
        const promise = response.rows.map(item => {
          return getCarrier(item.transportUnit).then(response => {
            item.transportUnit = response.data.carrierName;
          });
        });

        Promise.all(promise).then(() => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      });

      listCarrier(this.queryParams).then(response => {
        this.carrierList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleId: null,
        numberPlate: null,
        operatingLicense: null,
        driver: null,
        manufacturerModel: null,
        engineNumber: null,
        chassisHeight: null,
        loadBearing: null,
        vehicleNumber: null,
        driverLicense: null,
        manufacturer: null,
        idCard: null,
        carCapacity: null,
        carLength: null,
        maxNumber: null,
        transportUnit: null,
        city: null,
        pfcNumber: null,
        frameNumber: null,
        carColor: null,
        carWidth: null,
        mileage: null,
        createBy: null,
        createTime: null,
        editedBy: null,
        editedTime: null,
        status: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    onOpen() { },
    onClose() {
      this.$refs['elForm'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },
    handleConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        this.close()
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.reset();
      const vehicleId = row.vehicleId || this.ids
      getInfo(vehicleId).then(response => {
        response.data.transportUnit = parseInt(response.data.transportUnit)
        if (response.data.city != null && response.data.city != "") {
          response.data.city = response.data.city.split(",")
        }
        this.form = response.data;
        console.log(this.form, 'ww')
        this.title = "修改车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        let cityString;
        if (Array.isArray(this.form.city)) {
          cityString = this.form.city.join(',')
        }
        if (valid) {
          if (this.form.vehicleId != null) {
            if (Array.isArray(this.form.city)) {
              this.form.city = cityString
            }
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (Array.isArray(this.form.city)) {
              this.form.city = cityString
            }
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleIds = row.vehicleId || this.ids;
      this.$modal.confirm('是否确认删除车辆信息编号为"' + vehicleIds + '"的数据项？').then(function () {
        return delInfo(vehicleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.infoRow {
  margin-bottom: 10px;
}
</style>
